package com.gjy.learning.scala

import com.gjy.learning.scala.SparkReadOracle.oracleDFCreate
import org.apache.spark.sql.SparkSession
import org.apache.spark.util.LongAccumulator

import scala.math.BigDecimal.javaBigDecimal2bigDecimal

object AccumulatorExample {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("AccumulatorExample")
      .master("local[*]")
      .getOrCreate()
    val emp = oracleDFCreate(spark, "emp")

    emp.show()

    val sc = spark.sparkContext

    // 在Driver端创建累加器
    val counter: LongAccumulator = sc.longAccumulator("MyCounter")

    var cnt = 0
    emp.foreach { row =>
      if (row.getDecimal(7).toInt == 20) {
        cnt+=1
        counter.add(1)
      }
    }
    emp.collect()
    println(s"满足特定条件的记录数量: ${counter.value}")
    println(cnt)
    spark.stop()
  }
}